//
//  SHHomeViewController.swift
//  newShaohuoRen
//
//  Created by hujinbiao on 2018/3/21.
//  Copyright © 2018年 hujinbiao. All rights reserved.
//

import UIKit

class SHHomeViewController: BaseViewController {
        
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        navigationController?.navigationBar.shadowImage = nil
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "捎货人"
        
        setNavbarBarbuttonItems()
        
        if UserTicketModel.sharedInstance.isLogin {
            setAddChildViewController()
        }else {
            let loginVc = LoginController()
            self.present(loginVc, animated: true, completion: nil)
        }
        
        //接受通知监听
        NotificationCenter.default.addObserver(self, selector:#selector(loginSuccess), name: NSNotification.Name(rawValue: "loginSuccess"), object: nil)
    }
    
    //通知处理函数
    @objc func loginSuccess(notification:NSNotification){
        
        DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
            self.setAddChildViewController()
        }
    }
    
    func setAddChildViewController() {
        
        if UserTicketModel.sharedInstance.meIsShipper {
            let receiveVc = ReceiveOrderViewController()
            receiveVc.removeFromParentViewController()
            receiveVc.view.removeFromSuperview()
            addChildViewController(receiveVc)
            view.addSubview(receiveVc.view)
            
        }else {
            let applyVc = ApplyViewController()
            applyVc.removeFromParentViewController()
            applyVc.view.removeFromSuperview()
            addChildViewController(applyVc)
            view.addSubview(applyVc.view)
        }
    }
    
    /**
     设置 导航栏按钮
     */
    func setNavbarBarbuttonItems() -> Void {
        
        let leftMsgButton = UIButton(type: UIButtonType.custom)
        leftMsgButton.setImage(UIImage(named: "icon_barbutton_me"), for: UIControlState())
        leftMsgButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
        let leftBarButtonItem = UIBarButtonItem(customView: leftMsgButton)
        self.navigationItem.addLeftBarButtonItem(leftBarButtonItem)
        leftMsgButton.addTarget(self, action: #selector(SELLeftMsgButton), for: UIControlEvents.touchUpInside)
        
        let rightMsgButton = UIButton(type: UIButtonType.custom)
        rightMsgButton.setImage(UIImage(named: "icon_barbutton_msg"), for: UIControlState())
        rightMsgButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
        let rightBarButtonItem = UIBarButtonItem(customView: rightMsgButton)
        self.navigationItem.addRightBarButtonItem(rightBarButtonItem)
        rightMsgButton.addTarget(self, action: #selector(SELRightMsgButton), for: UIControlEvents.touchUpInside)
        
    }
    
    /**
     左边
     */
    @objc func SELLeftMsgButton() -> Void {
        if UserTicketModel.sharedInstance.isLogin {
            self.slideMenuController()?.openLeft()
        }
        else {
            let loginVc = LoginController()
            self.present(loginVc, animated: true, completion: nil)
            
        }
    }
    
   
    /**
     跳转到消息界面
     */
    @objc func SELRightMsgButton() -> Void {
        if UserTicketModel.sharedInstance.isLogin {
            let messageViewController = MessageViewController()
            messageViewController.title = "消息"
            navigationController?.pushViewController(messageViewController, animated: true)
        }
        else {
            let loginVc = LoginController()
            self.present(loginVc, animated: true, completion: nil)
            
        }
    }

}
